// JavaScript Document
window.onload = timer;
var billboardArray = new Array();
var t=0;//timer count

/*The sements of the billboard will be stored in a class*/
function billboardSegment(labelID, contentID){
  this.labelID = labelID;
  this.contentID = contentID;
  this.content = new Array();
  this.addContent = addContent;
  this.start = startloop;
}

function addContent(content) {
  var temp = this.content;
  temp.push(content);
  this.content = temp;
}

/*Function called by the billboardSegment object to start looping through its contents*/
function startloop(startIndex, displayTime) {
  loopContent(this.contentID, this.labelID, this.content, startIndex, displayTime);
}

// This loops through the content array
function loopContent(contentID, labelID, contentArray, startingIndex, displayTime){
  var size = contentArray.length;
  var i = startingIndex;

  if((t % displayTime) == 0){
    if(i >= size){
      i = 0; // reset array to index 0
      //document.getElementById(contentID).innerHTML = contentArray[i];
      setFadeOut(100, labelID, contentID, contentArray[i]);
      var fadeIn = function() {setFadeIn(0, labelID, contentID, contentArray[i-1]);};
      setTimeout(fadeIn, 3000);
      i++;  // increment index to queue up next content
    } else {
      //document.getElementById(contentID).innerHTML = contentArray[i];
      setFadeOut(100, labelID, contentID, contentArray[i]);
      var fadeIn = function() {setFadeIn(0, labelID, contentID, contentArray[i-1]);};
      setTimeout(fadeIn, 3000);
      i++;
    }
  }

  var callFunction = function(){loopContent(contentID, labelID, contentArray, i, displayTime);};

  setTimeout(callFunction, 1000);
  
}

/*The timer function.*/
function timer(){
  //checkTransitionTime(t);
  document.getElementById("timer").innerHTML = "Time Count:" + t;
  t++;
  setTimeout("timer()",1000);
}

/*Fade in function.*/
function setFadeIn(value, labelID, divID, content){
  var fadeIn = function() {setFadeIn(value, labelID, divID, content);};
  if (value <= 100) {
    setOpacityValue(value, labelID, divID, content);
    value += 10;
    setTimeout(fadeIn, 100); 
  }
}

/*Fade out function.*/
function setFadeOut(value, labelID, divID, content){
  var fadeOut = function() {setFadeOut(value, labelID, divID, content);};
  if (value >= 0) {
    setOpacityValue(value, labelID, divID, content);
    value -= 10;
    setTimeout(fadeOut, 100);
  }
}

/*sets the opacity value of a div.*/
function setOpacityValue(opacityValue, labelID, divID, content){
  document.getElementById(labelID).style.opacity = opacityValue/100;
  document.getElementById(divID).style.opacity = opacityValue/100; 
  if(opacityValue == 0){
    document.getElementById(labelID).innerHTML = labelID;
    document.getElementById(divID).innerHTML = content;
  }
}